package com.airsystem.pos.bengkel.controller;

import javax.accessibility.Accessible;
import javax.swing.JOptionPane;
import javax.swing.text.JTextComponent;

import org.apache.log4j.Logger;

import com.airsystem.pos.bengkel.entity.JenisLayanan;
import com.airsystem.pos.bengkel.interfaces.IMaster;
import com.airsystem.pos.bengkel.interfaces.IServices;
import com.airsystem.pos.bengkel.interfaces.IValidator;
import com.airsystem.pos.bengkel.model.MasterServiceTypeModel;
import com.airsystem.pos.bengkel.service.JenisLayananServices;
import com.airsystem.pos.bengkel.util.FormValidator;
import com.airsystem.pos.bengkel.view.dialog.MasterServiceTypeDialog;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class MasterServiceTypeController extends MasterServiceTypeModel implements IMaster {
	private static final Logger LOG = Logger.getLogger(MasterServiceTypeController.class.getSimpleName());

	private IValidator<Accessible> formValidator;

	private IServices<JenisLayanan> service;

	public MasterServiceTypeController() {
		super();

		service = new JenisLayananServices();

		formValidator = new FormValidator();
	}

	public void setup() {
		updateButton.setVisible(false);
		deleteButton.setVisible(false);
		idField.setVisible(false);
	}

	public void search() {
		dialogMasterServiceType = new MasterServiceTypeDialog();

		try {
			if (dialogMasterServiceType.getJenisLayanan() != null) {
				namaField.setText(dialogMasterServiceType.getJenisLayanan().getNama());
				idField.setText(dialogMasterServiceType.getJenisLayanan().getId_s());

				saveButton.setVisible(false);
				updateButton.setVisible(true);
				deleteButton.setVisible(true);
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
	}

	public void get() {
		throw new UnsupportedOperationException("Not implemented");
	}

	public void reset() {
		namaField.setText("");
		idField.setText("");
	}

	public void save() {
		if (!isFormValid(namaField)) {
			return;
		}

		try {
			service.saveOrUpdate(getComponentData());

			JOptionPane.showMessageDialog(this, "Simpan data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
			reset();
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Simpan data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void update() {
		if (!isFormValid(namaField, idField)) {
			return;
		}

		try {
			service.saveOrUpdate(getComponentData());

			JOptionPane.showMessageDialog(this, "Ubah data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
			cancel();
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Ubah data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void delete() {
		if (!isFormValid(idField)) {
			return;
		}

		try {
			service.delete(getComponentData());

			JOptionPane.showMessageDialog(this, "Hapus data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
			cancel();
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Hapus data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void cancel() {
		reset();

		try {
			saveButton.setVisible(true);
			updateButton.setVisible(false);
			deleteButton.setVisible(false);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
	}

	protected boolean isFormValid(JTextComponent... component) {
		try {
			formValidator.isValid(component);

			return true;
		} catch (IllegalArgumentException e) {
			JOptionPane.showMessageDialog(this, "Data kosong!", "Pesan", JOptionPane.WARNING_MESSAGE);
		}

		return false;
	}
}